package com.xsoft.sms.cmpp20.message.impl;

import com.xsoft.sms.cmpp20.message.ICMPP20Message;
import com.xsoft.sms.cmpp20.message.exception.CMPP20MessaageException;

import java.nio.ByteBuffer;

/**
 * package: {PACKAGE_NAME}
 * User: zhanglm
 * Date: 12-9-13
 * Time: 下午4:00
 * Description:
 */
public class CMPP20MessageHead implements ICMPP20Message {

	public CMPP20MessageHead() {
	}

	public CMPP20MessageHead(int totalLength, int cmdId, int sequenceId) {
		this.totalLength = totalLength;
		this.cmdId = cmdId;
		this.sequenceId = sequenceId;
	}

	public CMPP20MessageHead(int cmdId, int sequenceId) {
		this(0, cmdId, sequenceId);
	}

	public CMPP20MessageHead(CMPP20MessageHead messageHead) {
		this.totalLength = messageHead.getTotalLength();
		this.cmdId = messageHead.getCmdId();
		this.sequenceId = messageHead.getSequenceId();
	}

	@Override
	public byte[] toBuffer() {
		ByteBuffer buffer = ByteBuffer.allocate(CMPP20Message.CMPP20_HEAD_LENGTH);
		buffer.putInt(totalLength);
		buffer.putInt(cmdId);
		buffer.putInt(sequenceId);
		return buffer.array();

	}

	@Override
	public void fromBuffer(byte[] buffer) throws CMPP20MessaageException {
		ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
		try {
			totalLength = byteBuffer.getInt();
			cmdId = byteBuffer.getInt();
			sequenceId = byteBuffer.getInt();
		} catch (Exception ex) {
			throw new CMPP20MessaageException();
		}
	}

	public int getTotalLength() {
		return totalLength;
	}

	public void setTotalLength(int totalLength) {
		this.totalLength = totalLength;
	}

	public int getCmdId() {
		return cmdId;
	}

	public void setCmdId(int cmdId) {
		this.cmdId = cmdId;
	}

	public int getSequenceId() {
		return sequenceId;
	}

	public void setSequenceId(int sequenceId) {
		this.sequenceId = sequenceId;
	}

	private int totalLength;
	private int cmdId;
	private int sequenceId;

}
